package com.ethan.http;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {
    public static final String BASE_URL = "http://192.168.3.18:80";

    static RetrofitManager retrofitManager;
    public static RetrofitManager getRetrofitManager() {
        if (null == retrofitManager) {
            synchronized (RetrofitManager.class) {
                if (null == retrofitManager) {
                    return new RetrofitManager();
                }
            }
        }
        return retrofitManager;
    }

    private Retrofit retrofit;
    private OkHttpClient okHttpClient;
    public RetrofitManager() {
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(200L, TimeUnit.SECONDS)
                .readTimeout(200L, TimeUnit.SECONDS)
                .writeTimeout(200L, TimeUnit.SECONDS)
                .addInterceptor(new LoggingInterceptor())
                .addInterceptor(new TokenHeaderInterceptor())
                .build();
        retrofit =  new Retrofit.Builder()
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .build();
    }
    public static <T> T getServices(Class<T> sevices){
        return getRetrofitManager().retrofit.create(sevices);

    }
}
